package com.emex.app.web.service;

import com.emex.app.shared.data.PlanFact;
import com.emex.app.shared.data.PlanFactExample;
import com.emex.app.shared.persistance.PlanFactMapper;
import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Sergey Karavaev
 */
@Service
@Transactional
public class PlanFactService {
    
    @Autowired
    private PlanFactMapper planFactMapper;

    public PlanFact insertPlanFact(PlanFact record) {
        planFactMapper.insert(record);
        return record;
    }

    public PlanFact updatePlanFact(PlanFact record) {
        planFactMapper.updateByPrimaryKey(record);
        return record;
    }
    
    public PlanFact getCurrentMonthPlanFact() {
        Calendar currentDate = Calendar.getInstance();
        Integer currentMonthId = currentDate.get(Calendar.MONTH);
        PlanFactExample example = new PlanFactExample();
        example.createCriteria().andMonthIdEqualTo(currentMonthId);
        List<PlanFact> planFacts = planFactMapper.selectByExample(example);
        PlanFact planFact = null;
        if (null != planFacts && !planFacts.isEmpty()) {
            planFact = planFacts.get(0);
        }
        return planFact;
    }
}
